//
//  JQGuidanceVC.m
//  handworker
//
//  Created by apple on 15/9/17.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "JQGuidanceVC.h"
#import "Team9Header.h"
#import "AppDelegate.h"
#import <GPUImage.h>

@interface JQGuidanceVC () <UIScrollViewDelegate>
@property(nonatomic, retain) UIScrollView *scrollView;
@property(nonatomic, retain) UIImageView *startImage;
@property(nonatomic, retain) GPUImageGaussianBlurFilter *blurFilter;
@end

@implementation JQGuidanceVC
- (void)dealloc {
  _scrollView.delegate = nil;
}
- (void)viewDidLoad {
  [super viewDidLoad];
  self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
  [self.view addSubview:_scrollView];
  _scrollView.pagingEnabled = YES;
  _scrollView.showsVerticalScrollIndicator = NO;
  _scrollView.showsHorizontalScrollIndicator = NO;

  _scrollView.contentSize = CGSizeMake(4 * WIDTH, HEGHT);
  _scrollView.delegate = self;
  for (int i = 0; i < 3; i++) {
    @autoreleasepool {
      UIImageView *imageView = [self createImageView];
      NSString *imageStr = [NSString stringWithFormat:@"%d", i + 1];
      imageView.image = [UIImage imageNamed:imageStr];
      imageView.frame = CGRectMake(WIDTH * i, 0, WIDTH, HEGHT);
      [self.scrollView addSubview:imageView];
    }
  }
  @autoreleasepool {
    [self.scrollView addSubview:[self createLastImageView]];
  }
  [self startAnimation];
}
- (UIImageView *)createImageView {
  UIImageView *imageView = [[UIImageView alloc] init];
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  button.frame = CGRectMake(125 * WIDTH / 375, 500 * HEGHT / 667,
                            WIDTH - 250 * HEGHT / 667, 50 * HEGHT / 667);
  button.layer.cornerRadius = 20 * HEGHT / 667;
  button.layer.borderColor = [UIColor whiteColor].CGColor;
  button.layer.borderWidth = 1;
  button.titleLabel.font = [UIFont systemFontOfSize:24 * WIDTH / 375];
  [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  [button setTitle:@"下一页" forState:UIControlStateNormal];
  [button addTarget:self
                action:@selector(nextPage)
      forControlEvents:UIControlEventTouchUpInside];
  [imageView addSubview:button];
  imageView.userInteractionEnabled = YES;
    return imageView;
}
- (void)nextPage {
  [self.scrollView
      setContentOffset:CGPointMake(self.scrollView.contentOffset.x + WIDTH, 0)
              animated:YES];
}
- (UIImageView *)createLastImageView {
  UIImageView *imageView =
      [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4"]];
  imageView.frame = CGRectMake(WIDTH * 3, 0, WIDTH, HEGHT);
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(125 * WIDTH / 375, 500 * HEGHT / 667,
                              WIDTH - 250 * HEGHT / 667, 50 * HEGHT / 667);
    button.layer.cornerRadius = 20 * HEGHT / 667;
    button.layer.borderColor = [UIColor whiteColor].CGColor;
    button.layer.borderWidth = 1;
    button.titleLabel.font = [UIFont systemFontOfSize:24 * WIDTH / 375];
  [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  [button setTitle:@"进入应用" forState:UIControlStateNormal];
  [button addTarget:self
                action:@selector(run)
      forControlEvents:UIControlEventTouchUpInside];
  [imageView addSubview:button];
  imageView.userInteractionEnabled = YES;
  return imageView;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  CGPoint point = CGPointMake(WIDTH * 3, 0);
  if (scrollView.contentOffset.x == point.x) {
    dispatch_after(
        dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
        dispatch_get_main_queue(), ^{
          [self run];
        });
  }
}
- (void)run {
  AppDelegate *app = [UIApplication sharedApplication].delegate;
  [app reachability];
}
- (void)startAnimation {
  self.startImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
//  if ([JQEvengingType shareEvening].eveningFailling) {
//    self.startImage.image = [UIImage imageNamed:@"opendark"];
//  } else
    self.startImage.image = [UIImage imageNamed:@"open"];

  self.blurFilter = [[GPUImageGaussianBlurFilter alloc] init];
  self.blurFilter.blurRadiusInPixels = 0;
  self.startImage.image =
      [self.blurFilter imageByFilteringImage:self.startImage.image];

  // 讲设置好的UIVisualEffectView添加到要模糊的视图层上

  [self.view addSubview:self.startImage];
  dispatch_after(
      dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)),
      dispatch_get_main_queue(), ^{
        [self shopAnimation];
      });
}

- (void)shopAnimation {

  [UIView animateWithDuration:1.2
                   animations:^{

                     self.startImage.alpha = 0;
                     self.startImage.bounds =
                         CGRectMake(0, 0, self.view.bounds.size.width * 1.8,
                                    self.view.bounds.size.height * 1.8);

                   }];

  // 模糊
  [UIView animateWithDuration:0.0001
                   animations:^{
                     [UIView setAnimationDelay:0.4];
                     self.blurFilter.blurRadiusInPixels = 5;
                     self.startImage.image = [self.blurFilter
                         imageByFilteringImage:self.startImage.image];
                   }];
}
@end
